<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Item.php");
require_once("../../data/Comment.php");

/**
 * Display the form which allow to add a comment to an action
 * @param Item $p_record
 */
function addComment($p_record){
	?>
	<a href="comment_action.php?id=<?=base64_encode($p_record->getId()) ?>">Comment</a>
	<?php
}

/**
 * Display the specified action
 * @param Item $p_record
 */
function display($p_record){
	?>
	- <a href="display_action.php?id=<?=base64_encode($p_record->getId()) ?>">Display</a>
	<?php
}

/**
 * Display an HTML representation of an Item which is an action
 * @param Item $p_action an Item instance
 */
function displayAction($p_action){
?>
<p><b>Target date:</b>
<?=$p_action->getTargetDate() ?>
</p>
<b>Description:</b>
<div style="border:1px solid;background-color: white;padding: 5px"><?=$p_action->getDescription() ?></div>
<br/>
<table width="100%">
<tr>
<td>
<b>Created</b>: <?=$p_action->getCreated() ?><br/>
<b>By</b>: 
	<?php
		$l_creatingUser=$p_action->getCreatingUser_id_Ref();
	?>
	<a href="../profile/profile_display.php?id=<?=base64_encode($l_creatingUser->getId()) ?>"><?=$l_creatingUser->getFullName() ?></a>
</td>
<td>
<b>Allocated</b>: <?=$p_action->getAllocated() ?><br/>
<b>To</b>:
	<?php
		$l_allocatedUser=$p_action->getAllocatedUser_id_Ref();
		if(isset($l_allocatedUser)){
	?>
		<a href="../profile/profile_display.php?id=<?=base64_encode($l_allocatedUser->getId()) ?>"><?=$l_allocatedUser->getFullName() ?></a>
	<?php
		}
	?>
</td>
</tr>
<?php
	$l_comments=$p_action->getComments();
	if(count($l_comments)!=0){
?>
	<tr>
	<td colspan="2">
	<br/>
	<b>Comments:</b>
	<?php
		$l_commentsTable=new TableData("ActionComments");
		$l_commentsTable->setRecords($l_comments);
		$l_commentsTable->addHeader("creatingUserFullName","By");
		$l_commentsTable->addHeader("note","Comment");
		displayTable($l_commentsTable);
	?>
	</td>
	</tr>
<?php
	}
?>
</table>
<?
}


/**
 * Display an HTML representation of an activity for reporting purpose.<br/>
 * This display should contain:
 * <ul>
 * <li>A description of the activity</li>
 * <li>The last report (in date) attached to this activity</li>
 * <li>The opened actions associated with this activity</li>
 * </ul>
 * @param $p_activity Activity an Activity instance
 * @param $p_displayLinks boolean true if the "action" links must be displayed on the tables
 */
function displayActivityDashboard($p_activity, $p_displayLinks){
?>
<h2>Description</h2>
<p><b>In charge: </b>
<?

$l_responsibleUser=$p_activity->getResponsibleUser_Id_Ref();
if(isset($l_responsibleUser)){
	echo $l_responsibleUser->getFullName();
}else{
	echo "Not yet allocated";
}

?>
</p>
<div style="border:1px solid;background-color: white;"><?=$p_activity->getDescription() ?></div>
<br/>
<h2>Last report</h2>
<?php
$l_reportItems=Item::getFromCustomCriteria("WHERE Activity_Id=:Activity_Id AND ItemType_id=:ItemType_id AND ItemStatus_id=:ItemStatus_id group by Activity_id having min(created)",
array(
	"Activity_Id" => $p_activity->getId(),
	"ItemType_id" => Item::REPORTITEM_TYPE,
 	"ItemStatus_id" => Item::REPORTITEM_STATUS_CREATED));
if(count($l_reportItems)==1){
	$l_reportItem=array_shift($l_reportItems);
	?>
	<p>
	<b>Published:</b> <?=$l_reportItem->getCreated() ?><br/>
	<b>By:</b> <?=$l_reportItem->getCreatingUser_Id_Ref()->getFullName() ?>
	</p>
	<div style="border:1px solid;background-color: white;"><?=$l_reportItem->getDescription() ?></div>
	<?php
}else{
	echo "Not report available for this activity";
}
?>
<br/>
<?php
$l_openedActions=Item::getFromCustomCriteria("WHERE Activity_Id=:Activity_Id AND ItemType_id=:ItemType_id AND ItemStatus_id=:ItemStatus_id",
array(
	"Activity_Id" => $p_activity->getId(),
	"ItemType_id" => Item::ACTION_TYPE,
 	"ItemStatus_id" => Item::ACTION_STATUS_OPENED));

?>
<h2>Opened actions</h2>
There are <b><?=count($l_openedActions) ?> opened actions</b> for this activity.
<ul>
<?php
if(count($l_openedActions) !=0){
	$l_openedActionsTable=new TableData("OpenedActionsTable");
	$l_openedActionsTable->addHeader("title","Action");
	$l_openedActionsTable->addHeader("description","Description");
	$l_openedActionsTable->addHeader("activityLabel","Activity");
	$l_openedActionsTable->addHeader("overdueHtmlFlag","Overdue");
	$l_openedActionsTable->addHeader("commentCount","Comments");
	$l_openedActionsTable->addHeader("allocated","Allocated");
	if($p_displayLinks){
		$l_openedActionsTable->addAction("addComment");
		$l_openedActionsTable->addAction("display");
	}
	$l_openedActionsTable->setRecords($l_openedActions);
	
	displayTable($l_openedActionsTable);
}
?>
</ul>
<?php
}

?>